﻿
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Wise.Entities;

namespace NHI.Sync.Tools.Forms
{
    /// <summary>
    /// SourceConfig.xaml 的交互逻辑
    /// </summary>
    public partial class Form_SourceConfig : UserControl
    {
        bool Has_Changed_DataSource = false;
        public Form_SourceConfig()
        {
            InitializeComponent();
        }

        public void LoadData()
        {
            Data = MyDbConfig.Current.数据源;
            this.DataContext = Data;
            drp_Source_DB.ItemsSource = new List<string>() { Data.数据库名称 };
            drp_Source_DB.SelectedIndex = 0;
            radio_Windows.IsChecked = Data.使用Windows验证;
            radio_Sql.IsChecked = !Data.使用Windows验证;
            Data.PropertyChanged -= Data_PropertyChanged;
            Data.PropertyChanged += Data_PropertyChanged;
        }

        数据库配置 Data = null;

        private void Data_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            switch (e.PropertyName)
            {
                case "数据库名称":
                    break;
                default:
                    Data.数据库名称 = string.Empty;
                    drp_Source_DB.ItemsSource = new List<string>();
                    break;
            }
        }

        void InitDataBaseList()
        {
            List<string> tables = Data.GetDatasources();
            Dispatcher.Invoke(() =>
            {
                drp_Source_DB.ItemsSource = tables;
                Has_Changed_DataSource = true;
            });
        }


        void ShowError(string msg)
        {
            MainWindow.Current.ShowError(msg);
        }

        public bool IsValid(bool showError)
        {
            if (!IsValid_NO_DB(showError))
                return false;
            if (string.IsNullOrEmpty(Data.数据库名称))
            {
                if (showError)
                {
                    ShowError("请填写[数据库名称]！");
                }
                return false;
            }
            return true;
        }

        public bool IsValid_NO_DB(bool showError)
        {
            if (string.IsNullOrEmpty(Data.服务器名称))
            {
                if (showError)
                {
                    ShowError("请填写[服务器名称]！");
                }
                return false;
            }
            if (!Data.使用Windows验证)
            {
                if (string.IsNullOrEmpty(Data.用户名))
                {
                    if (showError)
                    {
                        ShowError("请填写[用户名]！");
                    }
                    return false;
                }
                if (string.IsNullOrEmpty(Data.密码))
                {
                    if (showError)
                    {
                        ShowError("请填写[密码]！");
                    }
                    return false;
                }
            }
            return true;
        }

        public void OnPrevious_Click(object o, EventArgs e)
        {
            MainWindow.Current.tab_0.IsSelected = true;
            MainWindow.Current.tab_0.IsEnabled = true;

        }
        public void OnNext_Click(object o, EventArgs e)
        {
            if (!IsValid(true))
                return;
            MainWindow.Current.tab_2.IsEnabled = true;
            MainWindow.Current.tab_2.IsSelected = true;
            MainWindow.Current.frame_2.LoadData();
            if (MyDbConfig.Current.是测试)
                MainWindow.Current.frame_2.OnNext_Click(o, e);
    }

        private void radio_Windows_Checked(object sender, RoutedEventArgs e)
        {
            pnl_Sql.IsEnabled = false;
            Data.使用Windows验证 = true;
        }

        private void radio_Sql_Checked(object sender, RoutedEventArgs e)
        {
            pnl_Sql.IsEnabled = true;
            Data.使用Windows验证 = false;
        }

        private void _LostFocus(object sender, RoutedEventArgs e)
        {

        }
        
        private void drp_Source_DB_GotFocus(object sender, RoutedEventArgs e)
        {
            if (!Has_Changed_DataSource || !drp_Source_DB.ItemsSource.GetEnumerator().MoveNext())
            {
                InitDataBaseList();
            }
        }
    }
}
